package net
{
	import flash3.net.ByteArrayPlus;
	import flash3.net.MsgPacket;
	
	import global.Define;
	
	import model.*;
	
	import util.RandomUtil;
	
	import vo.*;

	public class UserGroup extends MessageGroup
	{
		//用户数据包定义		
		public static const MDM_GR_USER:uint					=	202									//用户信息		
		public static const SUB_GR_USER_SIT_REQ:uint			=	1									//坐下请求
		public static const SUB_GR_USER_LOOKON_REQ:uint		=	2									//旁观请求
		public static const SUB_GR_USER_STANDUP_REQ:uint		=	3									//起立请求
		public static const SUB_GR_USER_LEFT_GAME_REQ:uint	=	4									//离开游戏	
		public static const SUB_GR_USER_LEFT_ROOM_REQ:uint	=	5									//离开房间	
		public static const SUB_GR_USER_COME:uint				=	100									//用户进入
		public static const SUB_GR_USER_STATUS	:uint		=	101									//用户状态
		public static const SUB_GR_USER_SCORE:uint			=	102									//用户分数
		public static const SUB_GR_SIT_FAILED:uint			=	103									//坐下失败		
		public static const SUB_GR_USER_CHAT:uint				=	200									//聊天消息
		public static const SUB_GR_USER_WISPER:uint			=	201									//私语消息
		public static const SUB_GR_USER_RULE:uint				=	202									//用户规则
		public static const SUB_GR_USER_INVITE	:uint		=	300									//邀请消息
		public static const SUB_GR_USER_INVITE_REQ	:uint	=	301									//邀请请求
			
		public static const SUB_GR_USER_AUTOSIT_REQ	:uint	=	400									//自动坐下请求
		public static const SUB_GR_SIT_SUCESS	:uint	=	104											//坐下成功
		public static const SUB_GR_USER_STATUS_REQ	:uint	=	500;								//用户状态请求
		
		public static const SUB_GR_USER_SUPPLY_OPTION:uint	=   702									//补给选项
		public static const SUB_GR_USER_MONEY_LESS:uint		=	801									//游戏币不足
		public static const SUB_GR_USER_SUPPLY_NEED:uint		= 	704									//需要补给
		public static const SUB_GR_USER_SUPPLY_REQ:uint		= 	701									//补给选项请求
		public static const SUB_GR_USER_SUPPLY_APPLY:uint		=	703									//补给
		public static const SUB_GR_USER_AGREE_ENTERFEE:uint	=	600									//用户交押金或者入场费
		public static const SUB_GR_USER_MONEYPOOL_REQ:uint	=	601									//奖金池请求
		public static const SUB_GR_USER_MONEYPOOL:uint		=	602									//奖金池回复
		public static const SUB_GR_USER_SUPPLY_APPLY_SUC:uint	=	705									//补给成功
		public static const SUB_GR_USER_RANKINFO_REQ:uint		=	900									//排名信息请求
		public static const SUB_GR_USER_RANKINFO:uint			=	901									//排名信息
		
		public static const DTP_USER_ACCOUNTS:uint			=	3									//用户帐号
		public static const DTP_USER_FACE_URL:uint				=	10								//头像连接
			
		public function UserGroup()
		{
			super();
		}
		
		override public function defMessage():void
		{
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_SIT_REQ, "坐下请求", onUserSitRequest));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_LOOKON_REQ, "旁观请求", onUserLookonRequest));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_STANDUP_REQ, "起立请求", onUserStandupRequest));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_LEFT_GAME_REQ, "离开游戏", onUserLeftGameRequest));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_LEFT_ROOM_REQ, "离开房间", onUserLeftRoomRequest));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_COME, "用户进入", onUserCome));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_STATUS, "用户状态", onUserStatus));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_SCORE, "用户分数", onUserScore));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_SIT_FAILED, "坐下失败", onUserSitFailed));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_CHAT, "聊天消息", onUserChat));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_WISPER, "私语消息", onUserWisper));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_RULE, "用户规则", onUserRule));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_INVITE, "邀请消息", onUserInvite));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_INVITE_REQ, "邀请请求", onUserInviteRequest));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_AUTOSIT_REQ, "自动坐下", onUserAutoSitRequest));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_SIT_SUCESS, "坐下成功", onUserSitSucess));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_STATUS_REQ, "用户状态请求", onUserStatusRequest));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_SUPPLY_OPTION, "补给选项", onUserSupplyOption));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_MONEY_LESS, "游戏币不足", onUserMoneyLess));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_SUPPLY_NEED, "需要补给", onUserSupplyNeedNotify));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_SUPPLY_REQ, "补给选项", onUserSupplyRequest));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_SUPPLY_APPLY, "请求补给", onUserSupplyApplyRequest));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_AGREE_ENTERFEE, "请求兑换", onUserAgreeEnterfeeRequest));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_MONEYPOOL_REQ, "奖金池请求", onUserMoneyPoolRequest));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_MONEYPOOL, "奖金池回复", onUserMoneyPoolReply));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_SUPPLY_APPLY_SUC, "补给成功", onUserSupplyApplySucReply));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_RANKINFO_REQ, "排名信息", onUserRankInfoRequest));
			addMessage(new MessageDef(MDM_GR_USER, SUB_GR_USER_RANKINFO, "排名信息", onUserRankInfoReply));
		}
		public function onUserStatusRequest(packet:MsgPacket):void
		{
			var userStatus:UserStatusRequest = packet._parm as UserStatusRequest;
			var bap:ByteArrayPlus = packet._data;
			bap.writeShort(userStatus.wSiteID);
			bap.writeShort(userStatus.wTableID);
			bap.writeShort(userStatus.wChairID);
			bap.writeShort(userStatus.cbUserType);
		}
		public function onUserSitRequest(packet:MsgPacket):void
		{
			var userSit:UserSit = packet._parm as UserSit;
			var bap:ByteArrayPlus = packet._data;
			bap.writeShort(userSit.wSiteID);
			bap.writeShort(userSit.wTableID);
			bap.writeShort(userSit.wChairID);
			bap.writeByte(userSit.szTablePass.length);
			bap.writeString({value:userSit.szTablePass, length:userSit.szTablePass.length});
		}
		public function onUserLookonRequest(packet:MsgPacket):void
		{
			
		}
		public function onUserStandupRequest(packet:MsgPacket):void
		{
			
		}
		public function onUserLeftGameRequest(packet:MsgPacket):void
		{
			
		}
		public function onUserLeftRoomRequest(packet:MsgPacket):void
		{
			
		}
		public function onUserCome(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			
			var wFaceID:uint 		= bap.readShort();
			var dwUserID:uint		= bap.readUnsignedInt();
			
			trace("onUserCome:", "dwUserID:", dwUserID);
			
			var userInfo:UserInfo = UserInfoModel.i.value(dwUserID);
			if(!userInfo)
			{
				userInfo = new UserInfo();
				UserInfoModel.i.add(dwUserID, userInfo);
			}
			userInfo.wFaceID 		= wFaceID
			userInfo.wFaceID 		= RandomUtil.randomRange(2, 6);
			userInfo.dwUserID		= dwUserID;
			userInfo.dwGameID		= bap.readUnsignedInt();
			userInfo.dwGroupID		= bap.readUnsignedInt();
			userInfo.dwUserRight	= bap.readUnsignedInt();
			userInfo.dwMasterRight	= bap.readUnsignedInt();
			userInfo.cbGender 		= bap.readByte();
			userInfo.cbMemberOrder 	= bap.readByte();
			userInfo.cbMasterOrder 	= bap.readByte();
			UserInfoModel.i.add(userInfo.dwUserID, userInfo);
			
			var userStatus:UserStatus = readUserStatus(bap, userInfo.dwUserID);
			var userScore:UserScore = readUserScore(bap, userInfo.dwUserID);
			
			//读取扩展信息
			while (bap.bytesAvailable > 0)
			{
				var wDataSize:uint = bap.readShort();
				var wDataType:uint = bap.readShort();
				
				switch(wDataType)
				{
					case DTP_USER_ACCOUNTS:
						userInfo.szAccounts = bap.readString(wDataSize);
						break;
					case DTP_USER_FACE_URL:
						userInfo.szImageUrl = bap.readString(wDataSize);
						break;
					default:
						bap.position += wDataSize;
						break;
				}
			}
			
			if(userInfo.dwUserID == MeInfoModel.i.userInfo.dwUserID)
			{
				MeInfoModel.i.userInfo = userInfo;
				MeInfoModel.i.userScore = userScore;
				MeInfoModel.i.userStatus = userStatus;
			}
		}
		public function onUserStatus(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			var dwUserID:uint = bap.readUnsignedInt();
			packet._parm = readUserStatus(bap, dwUserID);
		}
		public function onUserScore(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			var dwUserID:uint = bap.readUnsignedInt();
			var userScore:UserScore = readUserScore(bap, dwUserID);
			if(dwUserID == MeInfoModel.i.userInfo.dwUserID)
				MeInfoModel.i.userScore = userScore;
			packet._parm = dwUserID;
		}
		public function onUserSitFailed(packet:MsgPacket):void
		{
			
		}
		public function onUserChat(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			var userChat:UserChat = null;
			if(packet._type == MsgPacket.MSG_SEND)
			{
				userChat = packet._parm as UserChat;
				var bpl:ByteArrayPlus = new ByteArrayPlus();
				bpl.writeString({value:userChat.szChatMessage},true);
				bap.writeShort(bpl.length);
				bap.writeUnsignedInt(userChat.crFontColor);
				bap.writeUnsignedInt(userChat.dwSendUserID);
				bap.writeUnsignedInt(userChat.dwTargetUserID);
				bap.writeString({value:userChat.szChatMessage},true);
			}
			else
			{
				userChat = new UserChat();
				userChat.wChatLength = bap.readShort();
				userChat.crFontColor = bap.readUnsignedInt();
				userChat.dwSendUserID = bap.readUnsignedInt();
				userChat.dwTargetUserID = bap.readUnsignedInt();
				userChat.szChatMessage = bap.readMultiByte(userChat.wChatLength, "utf-8");
				packet._parm = userChat;
			}
			
		}
		public function onUserWisper(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			var userChat:UserChat = null;
			if(packet._type == MsgPacket.MSG_SEND)
			{
				userChat = packet._parm as UserChat;
				var bpl:ByteArrayPlus = new ByteArrayPlus();
				bpl.writeString({value:userChat.szChatMessage},true);
				bap.writeShort(bpl.length);
				bap.writeUnsignedInt(userChat.crFontColor);
				bap.writeUnsignedInt(userChat.dwSendUserID);
				bap.writeUnsignedInt(userChat.dwTargetUserID);
				bap.writeString({value:userChat.szChatMessage},true);
			}
			else
			{
				userChat = new UserChat();
				userChat.wChatLength = bap.readShort();
				userChat.crFontColor = bap.readUnsignedInt();
				userChat.dwSendUserID = bap.readUnsignedInt();
				userChat.dwTargetUserID = bap.readUnsignedInt();
				userChat.szChatMessage = bap.readMultiByte(userChat.wChatLength, "utf-8");
				packet._parm = userChat;
			}
		}
		public function onUserRule(packet:MsgPacket):void
		{
			
		}
		public function onUserInvite(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			
			var userInvite:UserInvite = new UserInvite()
			userInvite.wSiteID = bap.readShort();
			userInvite.wTableID = bap.readShort();
			userInvite.dwUserID = bap.readShort();
		}
		public function onUserInviteRequest(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			
			var userInvite:UserInvite = packet._parm as UserInvite;
			bap.writeShort(userInvite.wSiteID);
			bap.writeShort(userInvite.wTableID);
			bap.writeShort(userInvite.dwUserID);
		}
		
		public function onUserAutoSitRequest(packet:MsgPacket):void
		{
			
		}
		
		public function onUserSitSucess(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;

			var wSiteID:int = bap.readShort();
			var wTableID:int = bap.readShort();
			var wChairID:int = bap.readShort();
			var dwUserID:int = bap.readUnsignedInt();
			var cbUserStatus:int = UserStatus.STATE_SIT;
			
			var userStatus:UserStatus = UserStatusModel.i.value(dwUserID);
			if(!userStatus)
			{
				userStatus = new UserStatus();
				UserStatusModel.i.add(userStatus.dwUserID, userStatus);
			}
			userStatus.wSiteID = wSiteID;
			userStatus.wTableID = wTableID;
			userStatus.wChairID = wChairID;
			userStatus.dwUserID = dwUserID;
			userStatus.cbUserStatus = cbUserStatus;
			
			if(userStatus.cbUserStatus == UserStatus.STATE_FREE)
			{
				TableStatusModel.i.removeUserByUserID(dwUserID);
			}
			else if(userStatus.cbUserStatus == UserStatus.STATE_SIT)
			{
				TableStatusModel.i.addUserByUserStatus(userStatus);
			}
			
			if(MeInfoModel.i.userInfo.dwUserID == userStatus.dwUserID)
			{
				MeInfoModel.i.userStatus = userStatus;
				if(userStatus.cbUserStatus == UserStatus.STATE_FREE)
					GameModel.i.tableInfo = null;
				else if(userStatus.cbUserStatus == UserStatus.STATE_SIT)
					GameModel.i.tableInfo = TableStatusModel.i.value(userStatus.wTableID);
			}
			
			packet._parm = userStatus;
		}
		public function onUserSupplyOption(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			var optionCount:uint = bap.readShort();
			SupplyItemModel.i.clear();
			for(var i:uint = 0; i < optionCount; i++)
			{
				var supplyItem:SupplyItem = new SupplyItem();
				supplyItem.gold = bap.readInt();
				supplyItem.money = bap.readInt();
				SupplyItemModel.i.add(supplyItem);
			}
		}
		public function onUserMoneyLess(packet:MsgPacket):void
		{
			
		}
		public function onUserSupplyNeedNotify(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			GameModel.i.serverRunInfo.minMoney = bap.readInt();
		}
		public function onUserSupplyRequest(packet:MsgPacket):void
		{
			
		}
		public function onUserSupplyApplyRequest(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			bap.writeShort(packet._parm);
		}
		public function onUserAgreeEnterfeeRequest(packet:MsgPacket):void
		{
			
		}
		public function onUserMoneyPoolRequest(packet:MsgPacket):void
		{
			
		}
		
		public function onUserMoneyPoolReply(packet:MsgPacket):void
		{
 			var bap:ByteArrayPlus = packet._data;
			GameModel.i.serverRunInfo.prizesPool.readPrizePool(bap);
			var prizePool:MoneyPoolItem = GameModel.i.serverRunInfo.prizesPool;
			var listPoolItem1:Array = prizePool.listPoolItem;
			var listPoolItem2:Array = GameModel.i.poolItem.listPoolItem;
			for(var i:uint = 0; i < listPoolItem1.length; i++)
			{
				var poolItem1:ListPoolItem = listPoolItem1[i] as ListPoolItem;
				if(poolItem1)
				{
					var poolItem2:ListPoolItem = listPoolItem2[i] ? listPoolItem2[i] : (new ListPoolItem);
					poolItem2.money = poolItem1.money;
					poolItem2.gold= poolItem1.gold;
				}
				else
				{
					listPoolItem2[i] = null;
				}
			}
			
			packet._parm = prizePool;
		}
		
		public function onUserSupplyApplySucReply(packet:MsgPacket):void
		{
			
		}
		
		public function onUserRankInfoRequest(packet:MsgPacket):void
		{
			
		}
		public function onUserRankInfoReply(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			var serverRunInfo:ServerRunInfo = GameModel.i.serverRunInfo;
			serverRunInfo.rankInfo.read(bap);
			packet._parm = serverRunInfo.rankInfo;
		}
		
		public function readUserStatus(bap:ByteArrayPlus, dwUserID:uint):UserStatus
		{
			var userStatus:UserStatus = UserStatusModel.i.value(dwUserID);
			if(!userStatus)
			{
				userStatus = new UserStatus();
				UserStatusModel.i.add(dwUserID, userStatus);
			}
			
			userStatus.dwUserID = dwUserID;
			userStatus.wSiteID = bap.readShort();
			userStatus.wTableID = bap.readShort();
			userStatus.wChairID = bap.readShort();
			userStatus.cbUserStatus = bap.readByte();
			
			trace("UserStatus:", 
				"dwUserID:", userStatus.dwUserID, 
				"wSiteID:", userStatus.wSiteID, 
				"wTableID:", userStatus.wTableID, 
				"wChairID:", userStatus.wChairID, 
				"cbUserStatus:", userStatus.cbUserStatus);
			
			if(userStatus.cbUserStatus == UserStatus.STATE_FREE)
			{
				TableStatusModel.i.removeUserByUserID(userStatus.dwUserID);
			}
			else if(userStatus.cbUserStatus > UserStatus.STATE_FREE)
			{
				TableStatusModel.i.addUserByUserStatus(userStatus);
			}

			if(MeInfoModel.i.userInfo.dwUserID == userStatus.dwUserID)
			{
				MeInfoModel.i.userStatus = userStatus;
				if(userStatus.cbUserStatus == UserStatus.STATE_FREE)
					GameModel.i.tableInfo = null;
				else if(userStatus.cbUserStatus == UserStatus.STATE_SIT)
					GameModel.i.tableInfo = TableStatusModel.i.value(userStatus.wTableID);
			}
			
			return userStatus;
		}
		public function readUserScore(bap:ByteArrayPlus, dwUserID:uint):UserScore
		{
			var userScore:UserScore = UserScoreModel.i.value(dwUserID);
			if(!userScore)
			{
				userScore = new UserScore();
				UserScoreModel.i.add(dwUserID, userScore);
			}
			userScore.read(bap);
			return userScore;
		}
	}
}